package com.patrol.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.patrol.pojo.User;
import com.patrol.service.UserService;
import com.patrol.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author zsc
 * @description: 用户管理接口
 */
@CrossOrigin
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 根据ID查询用户
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID查询用户")
    @GetMapping("selectUserById/{id}")
    public Result selectUserById(@PathVariable Integer id){
        User user = userService.getById(id);
        return Result.ok(user);
    }

    /**
     * 分页查询用户
     * @param current 当前页
     * @param limit 每页记录数
     * @return
     *
     * 带条件查询
     * TODO
     */
    @ApiOperation(value = "分页查询用户")
    @PostMapping("findPagePoint/{current}/{limit}")
    public Result selectPagePoint(@PathVariable long current, @PathVariable long limit){
        Page<User> page = new Page<>(current,limit);
        Page<User> pageUser = userService.page(page);
        return Result.ok(pageUser);
    }

    /**
     * 查询所有用户
     * @return
     */
    @ApiOperation(value = "查询所有用户")
    @GetMapping("selectAll")
    public Result selectAll(){
        List<User> list = userService.list();
        return Result.ok(list);
    }

    /**
     * 根据ID删除用户
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID删除用户")
    @DeleteMapping("{id}")
    public Result deleteUserById(@PathVariable Integer id){
        boolean flag = userService.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 批量逻辑删除用户
     * @param idList
     * @return
     */
    @ApiOperation(value = "批量逻辑删除用户")
    @DeleteMapping("batchRemove")
    public Result batchRemoveUser(@RequestBody List<Long> idList){
        userService.removeByIds(idList);
        return Result.ok();
    }

    /**
     * 根据ID修改用户信息
     * @param user
     * @return
     */
    @ApiOperation(value = "根据ID修改用户信息")
    @PostMapping("updateUser")
    public Result updateUserById(@RequestBody User user){
        boolean flag = userService.updateById(user);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @ApiOperation(value = "添加用户")
    @PostMapping("insertUser")
    public Result insertUser(@RequestBody User user){
        boolean flag = userService.save(user);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }


    /**
     * 导入Excle表，批量添加用户信息
     * TODO
     */

}
